/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.discount.platform.listener;

import com.yami.shop.bean.event.RemoveDiscountProdByIdsEvent;
import com.yami.shop.discount.comment.service.DiscountService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 修改满减活动的可用商品列表
 *
 * @author lhd
 */
@Component("updateDiscountListener")
@AllArgsConstructor
public class UpdateDiscountProdListener {
    private final DiscountService discountService;

    /**
     * 修改满减活动的可用商品列表
     */
    @EventListener(RemoveDiscountProdByIdsEvent.class)
    @Transactional(rollbackFor = Exception.class)
    public void updateDiscountListener(RemoveDiscountProdByIdsEvent event) {
        List<Long> ids = discountService.updateDiscountProdByIds(event.getProdIds());
        if(CollectionUtils.isEmpty(ids)){
            return;
        }
        for (Long id : ids) {
            // 移除缓存
            discountService.removeDiscountAndItemAndProdCacheById(id);
        }
    }
}
